package api

import (
	"github.com/gin-gonic/gin"
	"homework/global"
	"homework/intelnal/model"
	"net/http"
)

func Login(c *gin.Context) {

	//用户登录
	//请求参数
	// 密码验证
	//登录成功
	var form User
	err := c.ShouldBind(&form)
	if err != nil {
		c.JSON(http.StatusBadRequest, "请求参数错误")
		return
	}
	var user model.User
	err = global.DB.Where("user_name", form.UserName).Limit(1).Find(&user).Error
	if err != nil {
		c.JSON(http.StatusBadRequest, "查询失败")
		return
	}
	if user.ID == 0 {
		c.JSON(http.StatusBadRequest, "用户不存在")
		return
	}
	if user.Password != form.Password {
		c.JSON(http.StatusBadRequest, "密码错误")
		return
	}
	c.JSON(http.StatusOK, "登录成功")
}
